%-------------------------------------------------------------------------;
% Table 2: Holding Period Returns and Sharpe Ratios;
% Figure 3: Annualized Standard Deviation Across Different Holding Periods;
% Figure 4: Annualized Sharpe Ratio Across Different Holding Periods;
%-------------------------------------------------------------------------;

clear;
clc;
close all;

%-------------------;
% Import data;
%-------------------;

filename='Main_data_1996_2022';
[data,txt,raw]=xlsread(filename);

dates=txt(2:end,1);

%Index data;
SP_500=data(:,1);           %SP 500 index price level;
Div_m=data(:,2);            %Monthly index dividends;
Div_a=data(:,3);            %Annual index dividends;
SP_ret=data(2:end,4);       %Monthly return on SP 500 index;

%Dividend strip data;
Div_strip_12=data(:,5);     %Price of 12-month dividend strip;
Div_strip_ret=data(2:end,6);%Monthly return on dividend strip strategy;

%Indicated dividends;
Ind_div=data(1:end,7);  

%Bond returns;
Bond_2y=data(2:end,8);      %Monthly return on 2-year Treasury bond;
Bond_10y=data(2:end,9);     %Monthly return on 10-year Treasury bond;

%Fama-French data;
Mkt_Rf=data(2:end,10);      %Market factor;
Rf=data(2:end,11);          %Risk-free rate;

%Define dividend-to-price ratios;
DP_sp=Div_a./SP_500;
DP_strip=Div_a./Div_strip_12;

%Define indicated dividend growthh;
Ind_dg=(Ind_div./Div_a)-1;     

%Take logarithms of all the main variables;
ret_sp=log(SP_ret+1);
ret_strip=log(Div_strip_ret+1);

bond_2y=log(Bond_2y+1);  
bond_10y=log(Bond_10y+1);

mkt_rf=log(Mkt_Rf+1);
rf=log(Rf+1);

dp_sp=log(DP_sp);
dp_strip=log(DP_strip);

ind_dg=log(Ind_dg+1);

%-----------------------------------------;
% Table 2, Panel A: In excess of the rf;
%-----------------------------------------;

ret_sp_ex=ret_sp-rf; 
ret_strip_ex=ret_strip-rf;

%For calculation of SRs with constant mean;
ret_sp_mean=mean(ret_sp_ex);
ret_strip_mean=mean(ret_strip_ex);

%To store results;
pval_overlap_SR=zeros(1,36);
pval_overlap_demeaned_SR=zeros(1,36);
pval_overlap_SR_const=zeros(1,36);
pval_overlap_demeaned_SR_const=zeros(1,36);
Tab_2A=zeros([11,36]);

for h=1:36
    
ret_sp_h=filter(ones(h,1),1,ret_sp_ex);
ret_strip_h=filter(ones(h,1),1,ret_strip_ex);

ret_sp_h=ret_sp_h(h:end);
ret_strip_h=ret_strip_h(h:end);

Tab_2A(1:2,h)=[mean(ret_sp_h)*100/(h/12);std(ret_sp_h)*100/sqrt(h/12)];
Tab_2A(3,h)=Tab_2A(1,h)/Tab_2A(2,h);
Tab_2A(4,h)=(ret_sp_mean*100/(1/12))/Tab_2A(2,h); %Constant mean;

Tab_2A(6:7,h)=[mean(ret_strip_h)*100/(h/12);std(ret_strip_h)*100/sqrt(h/12)];
Tab_2A(8,h)=Tab_2A(6,h)/Tab_2A(7,h);
Tab_2A(11,h)=(ret_strip_mean*100/(1/12))/Tab_2A(7,h); %Constant mean;

%-----------------------------------;
%Tests on standard Sharpe ratios;
%-----------------------------------;

%Difference in Sharpe ratios;
[~,pval_overlap_SR(h),~,~]=sharpeHAC([ret_strip_h ret_sp_h],'QS');
Tab_2A(9,h)=pval_overlap_SR(h);

%Difference in Sharpe ratios wrt. the demeaned market return;
ret_sp_h_demeaned=ret_sp_h - mean(ret_sp_h)*ones(size(ret_sp_h,1),1);
[~,pval_overlap_demeaned_SR(h),~,~]=sharpeHAC([ret_strip_h ret_sp_h_demeaned],'QS');
Tab_2A(10,h)=pval_overlap_demeaned_SR(h);

%------------------------------------------------------------------------;
%Tests on Sharpe ratios with constant mean (demean at the given holding 
%period and add back monthly mean for dividend strips and the market);
%------------------------------------------------------------------------;

ret_sp_h_const=ret_sp_h-mean(ret_sp_h)*ones(size(ret_sp_h,1),1)+ret_sp_mean*h*ones(size(ret_sp_h,1),1);
ret_strip_h_const=ret_strip_h-mean(ret_strip_h)*ones(size(ret_strip_h,1),1)+ret_strip_mean*h*ones(size(ret_strip_h,1),1);

%Difference in Sharpe ratios;
[~,pval_overlap_SR_const(h),~,~]=sharpeHAC([ret_strip_h_const ret_sp_h_const],'QS');
Tab_2A(12,h)=pval_overlap_SR_const(h);

%Difference in Sharpe ratios wrt. the demeaned market return;
ret_sp_h_demeaned=ret_sp_h-mean(ret_sp_h)*ones(size(ret_sp_h,1),1);

[~,pval_overlap_demeaned_SR_const(h),~,~]=sharpeHAC([ret_strip_h_const ret_sp_h_demeaned],'QS');
Tab_2A(13,h)=pval_overlap_demeaned_SR_const(h);

Tab_2A(14,h)=[length(ret_strip_h)];
end

Tab_2A=Tab_2A(1:end,[1 6 12 18 24 30 36]);

%------------------------------------------------------;
% Table 2, Panel B: In excess of the Treasury returns;
%------------------------------------------------------;

ret_sp_ex=ret_sp-bond_10y; 
ret_strip_ex=ret_strip-bond_2y;

%For calculation of SRs with constant mean;
ret_sp_mean=mean(ret_sp_ex);
ret_strip_mean=mean(ret_strip_ex);

%To store results;
pval_overlap_SR=zeros(1,36);
pval_overlap_demeaned_SR=zeros(1,36);
pval_overlap_SR_const=zeros(1,36);
pval_overlap_demeaned_SR_const=zeros(1,36);
Tab_2B=zeros([11,36]);

for h=1:36
    
ret_sp_h=filter(ones(h,1),1,ret_sp_ex);
ret_strip_h=filter(ones(h,1),1,ret_strip_ex);

ret_sp_h=ret_sp_h(h:end);
ret_strip_h=ret_strip_h(h:end);

Tab_2B(1:2,h)=[mean(ret_sp_h)*100/(h/12);std(ret_sp_h)*100/sqrt(h/12)];
Tab_2B(3,h)=Tab_2B(1,h)/Tab_2B(2,h);
Tab_2B(4,h)=(ret_sp_mean*100/(1/12))/Tab_2B(2,h); %Constant mean;

Tab_2B(6:7,h)=[mean(ret_strip_h)*100/(h/12);std(ret_strip_h)*100/sqrt(h/12)];
Tab_2B(8,h)=Tab_2B(6,h)/Tab_2B(7,h);
Tab_2B(11,h)=(ret_strip_mean*100/(1/12))/Tab_2B(7,h); %Constant mean;

%-----------------------------------;
%Tests on standard Sharpe ratios;
%-----------------------------------;

%Difference in Sharpe ratios;
[~,pval_overlap_SR(h),~,~]=sharpeHAC([ret_strip_h ret_sp_h],'QS');
Tab_2B(9,h)=pval_overlap_SR(h);

%Difference in Sharpe ratios wrt. the demeaned market return;
ret_sp_h_demeaned=ret_sp_h - mean(ret_sp_h)*ones(size(ret_sp_h,1),1);
[~,pval_overlap_demeaned_SR(h),~,~]=sharpeHAC([ret_strip_h ret_sp_h_demeaned],'QS');
Tab_2B(10,h)=pval_overlap_demeaned_SR(h);

%------------------------------------------------------------------------;
%Tests on Sharpe ratios with constant mean (demean at the given holding 
%period and add back monthly mean for dividend strips and the market);
%------------------------------------------------------------------------;

ret_sp_h_const=ret_sp_h-mean(ret_sp_h)*ones(size(ret_sp_h,1),1)+ret_sp_mean*h*ones(size(ret_sp_h,1),1);
ret_strip_h_const=ret_strip_h-mean(ret_strip_h)*ones(size(ret_strip_h,1),1)+ret_strip_mean*h*ones(size(ret_strip_h,1),1);

%Difference in Sharpe ratios;
[~,pval_overlap_SR_const(h),~,~]=sharpeHAC([ret_strip_h_const ret_sp_h_const],'QS');
Tab_2B(12,h)=pval_overlap_SR_const(h);

%Difference in Sharpe ratios wrt. the demeaned market return;
ret_sp_h_demeaned=ret_sp_h-mean(ret_sp_h)*ones(size(ret_sp_h,1),1);

[~,pval_overlap_demeaned_SR_const(h),~,~]=sharpeHAC([ret_strip_h_const ret_sp_h_demeaned],'QS');
Tab_2B(13,h)=pval_overlap_demeaned_SR_const(h);

Tab_2B(14,h)=[length(ret_strip_h)];
end

Tab_2B=Tab_2B(1:end,[1 6 12 18 24 30 36]);

disp('Table 2, Panel A')
disp(Tab_2A)

disp('Table 2, Panel B')
disp(Tab_2B)

%------------------------------------------;
%Figure 3: Annualized Standard Deviation;
%------------------------------------------;

horizontal_axis=[1,6,12,18,24,30,36];

%Panel A: In excess of the rf;

vol_mkt=Tab_2A(2,:)';
vol_strip=Tab_2A(7,:)';

figure(1)
plot(horizontal_axis,vol_mkt,'-.','LineWidth',1)
hold on;
plot(horizontal_axis,vol_strip,'-','LineWidth',1)
hold on;
legend('Market - rf','Dividend strip - rf')
xlabel('Holding period in months')
ylabel('Annualized volatility in %')
set(gca,'FontSize',8)
axis([1 36 10 35]);
xticks([1 6 12 18 24 30 36])

%Panel B: In excess of the Treasury returns;

vol_mkt=Tab_2B(2,:)';
vol_strip=Tab_2B(7,:)';

figure(2)
plot(horizontal_axis,vol_mkt,'-.','LineWidth',1)
hold on;
plot(horizontal_axis,vol_strip,'-','LineWidth',1)
hold on;
legend('Market - Treasury 10y','Dividend strip - Treasury 2y')
xlabel('Holding period in months')
ylabel('Annualized volatility in %')
set(gca,'FontSize',8)
axis([1 36 10 35]);
xticks([1 6 12 18 24 30 36])

%--------------------------------------;
%Figure 4: Annualized Sharpe ratios;
%--------------------------------------;

%Panel A: In excess of the rf;

SR_mkt=Tab_2A(3,:)';
SR_strip=Tab_2A(8,:)';

figure(3)
plot(horizontal_axis,SR_mkt,'-.','LineWidth',1)
hold on;
plot(horizontal_axis,SR_strip,'-','LineWidth',1)
hold on;
legend('Market - rf','Dividend strip - rf')
xlabel('Holding period in months')
ylabel('Annualized Sharpe ratio')
set(gca,'FontSize',8)
axis([1 36 0.1 0.55]);
xticks([1 6 12 18 24 30 36])

%Panel B: In excess of the Treasury returns;

SR_mkt=Tab_2B(3,:)';
SR_strip=Tab_2B(8,:)';

figure(4)
plot(horizontal_axis,SR_mkt,'-.','LineWidth',1)
hold on;
plot(horizontal_axis,SR_strip,'-','LineWidth',1)
hold on;
legend('Market - Treasury 10y','Dividend strip - Treasury 2y')
xlabel('Holding period in months')
ylabel('Annualized Sharpe ratio')
set(gca,'FontSize',8)
axis([1 36 0.1 0.55]);
xticks([1 6 12 18 24 30 36])


